package com.weijian.dal.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.weijian.dal.entity.UserFollow;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 用户关注关系Mapper
 * 使用原生SQL实现查询操作
 */
@Mapper
public interface UserFollowMapper extends BaseMapper<UserFollow> {

    /**
     * 获取用户的关注用户ID列表
     */
    @Select("SELECT following_id FROM user_follows WHERE follower_id = #{userId} AND deleted = 0 ORDER BY create_time DESC")
    List<Long> selectFollowingIds(@Param("userId") Long userId);

    /**
     * 获取用户的粉丝用户ID列表
     */
    @Select("SELECT follower_id FROM user_follows WHERE following_id = #{userId} AND deleted = 0 ORDER BY create_time DESC")
    List<Long> selectFollowerIds(@Param("userId") Long userId);

    /**
     * 获取用户的关注数量
     */
    @Select("SELECT COUNT(*) FROM user_follows WHERE follower_id = #{userId} AND deleted = 0")
    Long selectFollowingCount(@Param("userId") Long userId);

    /**
     * 获取用户的粉丝数量
     */
    @Select("SELECT COUNT(*) FROM user_follows WHERE following_id = #{userId} AND deleted = 0")
    Long selectFollowersCount(@Param("userId") Long userId);

    /**
     * 获取关注数量排行榜
     */
    @Select("SELECT follower_id FROM user_follows WHERE deleted = 0 GROUP BY follower_id ORDER BY COUNT(*) DESC LIMIT #{limit}")
    List<Long> selectTopFollowingUsers(@Param("limit") int limit);

    /**
     * 获取共同好友数量
     */
    @Select("SELECT COUNT(DISTINCT f1.following_id) FROM user_follows f1 " +
            "INNER JOIN user_follows f2 ON f1.following_id = f2.following_id " +
            "WHERE f1.follower_id = #{userId1} AND f2.follower_id = #{userId2} AND f1.deleted = 0 AND f2.deleted = 0")
    Long selectMutualFriendsCount(@Param("userId1") Long userId1, @Param("userId2") Long userId2);

    /**
     * 获取共同好友ID列表
     */
    @Select("SELECT DISTINCT f1.following_id FROM user_follows f1 " +
            "INNER JOIN user_follows f2 ON f1.following_id = f2.following_id " +
            "WHERE f1.follower_id = #{userId1} AND f2.follower_id = #{userId2} AND f1.deleted = 0 AND f2.deleted = 0")
    List<Long> selectMutualFriends(@Param("userId1") Long userId1, @Param("userId2") Long userId2);

    /**
     * 获取用户的关注关系详情
     */
    @Select("SELECT * FROM user_follows WHERE follower_id = #{followerId} AND following_id = #{followingId} AND deleted = 0")
    List<UserFollow> selectFollowDetail(@Param("followerId") Long followerId, @Param("followingId") Long followingId);

    /**
     * 批量获取用户的关注状态
     */
    @Select({"<script>",
            "SELECT * FROM user_follows",
            "WHERE follower_id = #{followerId}",
            "AND following_id IN",
            "<foreach collection='followingIds' item='id' open='(' separator=',' close=')'>#{id}</foreach>",
            "AND deleted = 0",
            "</script>"})
    List<UserFollow> selectBatchFollowStatus(@Param("followerId") Long followerId, @Param("followingIds") List<Long> followingIds);
}